#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "list.h"

#define TEST_SIZE 1024

int main( int argc, char** argv)
{
    list_t A;
    listInit( &A);
    
    int tmp;
    
    int i;
    for ( i = 1; i < TEST_SIZE ; i++)
    {
        if ( !listPush_front( &A , i))
        {
            printf("List is full");
        }
    }
    if ( listSize( &A) != TEST_SIZE -1 )
    {
        printf("\n!!!TEST FAILED\n");
        return 0;
    }

    list_elem_t *iterator;
    listIterator( &A, &iterator);

    int T = 1;
    while ( T == 1 )
    {
        T = listNext( &iterator);
    }
    if ( iterator->next != NULL)
    {
        printf( "\n!!!TEST FAILED listNext\n");
        return 0;
    }

    listIterator( &A, &iterator);

    for ( i = 1; i < TEST_SIZE /2; i++)
    {
        listNext( &iterator);
    }

    list_elem_t *iter = NULL;

    
    if ( listDelElem( &A, &iter) !=0)
    {
        printf("\n!!!TEST FAILED listDelElem1\n");
        return 0;        
    }
    if ( listDelElem( &A, &iter+123) != -1)
    {
        printf("\n!!!TEST FAILED listDelElem2\n");
        return 0;
    }
    // середина
    if ( listDelElem( &A, &iterator) != 1)
    {
        printf("\n!!!TEST FAILED listDelElem3\n");
        return 0;        
    }
    // первый
    listIterator( &A, &iterator);
    if ( listDelElem( &A, &iterator) != 1)
    {
        printf("\n!!!TEST FAILED listDelElem4\n");
        return 0;
    }
    // последний
    while( listNext( &iterator ) != -1)
    {}
    if ( listDelElem( &A, &iterator) != 1)
    {
        printf("\n!!!TEST FAILED listDelElem5\n");
        return 0;
    }

    if ( listSize( &A) != TEST_SIZE -4)
    {
        printf("\n!!!TEST FAILED listSize != TEST_SIZE\t%d\t%d\n",listSize( &A),TEST_SIZE-4);
        return 0;
    }

        
    for ( i = 2; i < TEST_SIZE +2; i++)
    {
        if ( i == TEST_SIZE/2 )
        {
            i++;
        }
        if ( listPop_front( &A, &tmp) == 1)
        {
            if ( (TEST_SIZE - i) != tmp)
            {
                printf("Elem[ %d] = %d\t, but should be %d",i,tmp,TEST_SIZE - i);
                printf("\n!!!TEST FAILED\n");
                return 0;
            }
        }
        else
        {   
            if ( listSize( &A) <= 0  )
            {
                //printf("List is empty\n");
            }
            else
            {
                printf("List isn't empy, but listSize have returned > 0");
                printf("\n!!!TEST FAILED\n");
                return 0;
            }
        }
    }


    listDel( &A);
    if ( listSize( &A) == -1)
    {
    }
    else
    {
        printf("List isn't empty after listDel");
        printf("\n!!!TEST FAILED\n");
        return 0;
    }

    if ( listIterator( &A , &iterator))
    {
        printf("List empty but iterator have created");
        printf("\n!!!TEST FAILED\n");
        return 0;
    }

    printf("\n!!!TEST PASSED\n");
    return 0;
}